package com.bucg.cjcloud.gb28181.controller;


import com.bucg.cjcloud.gb28181.common.entity.ItemEntity;
import com.bucg.cjcloud.gb28181.common.service.DeviceMangeService;
import com.bucg.cjcloud.gb28181.media.cache.PlayCache;
import com.bucg.cjcloud.gb28181.media.conf.MediaProperties;
import com.bucg.cjcloud.gb28181.media.model.NoneReader;
import com.bucg.cjcloud.gb28181.media.model.NotFound;
import com.bucg.cjcloud.gb28181.media.model.StreamChanged;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * HookController..
 * <p>
 * ZLMediaKit回调使用
 *
 * @author Lizhong
 * @date 2019/12/13
 */
@RestController
@RequestMapping("/index/hook")
public class HookController {

    @Autowired
    DeviceMangeService deviceMangeService;

    @Autowired
    SipController gbController;

    @Autowired
    MediaProperties mediaProperties;

    private String APP_TYPE;

    @PostConstruct
    void init() {
        APP_TYPE = mediaProperties.getAppType();
    }


    @PostMapping("/on_stream_none_reader")
    public ResponseEntity<String> onNoneReader(@RequestBody NoneReader noneReader) {


        String app = noneReader.getApp();
        String stream = noneReader.getStream();
        if (APP_TYPE.equalsIgnoreCase(app)) ResponseEntity.ok(app + "/" + stream + ": not is {" + APP_TYPE + "}");

        int ssrc = Integer.parseInt(stream, 16);

        ItemEntity item = deviceMangeService.getItem(ssrc);
        if (item == null) return ResponseEntity.ok(app + "/" + stream + ": not found }");

        gbController.sendRequest("bye", item.getDeviceId(), true);


        return ResponseEntity.ok("success");
    }

    @PostMapping("/on_stream_not_found")
    public ResponseEntity<String> onNotFound(@RequestBody NotFound noFound) {
        String app = noFound.getApp();
        String stream = noFound.getStream();
        if (!APP_TYPE.equalsIgnoreCase(app))
            return ResponseEntity.ok(app + "/" + stream + ": not is {" + APP_TYPE + "}");

        int ssrc = Integer.parseInt(stream, 16);

        ItemEntity item = deviceMangeService.getItem(ssrc);
        if (item == null) return ResponseEntity.ok(app + "/" + stream + ": not found }");

        gbController.sendRequest("play", item.getDeviceId(), true);
        return ResponseEntity.ok("success");
    }

    /**
     * 清除已经发起播放缓存
     *
     * @param streamChanged
     * @return
     */
    @PostMapping("/on_stream_changed")
    public ResponseEntity<StreamChanged> onChanged(@RequestBody StreamChanged streamChanged) {

        boolean regist = streamChanged.isRegist();
        String app = streamChanged.getApp();
        String stream = streamChanged.getStream();
        if (!APP_TYPE.equalsIgnoreCase(app)) return ResponseEntity.ok(streamChanged);

        int ssrc = Integer.parseInt(stream, 16);
        if (regist) return ResponseEntity.ok(streamChanged);

        // regist ==false 移除播放列表
        PlayCache.SSRC_PLAYING.remove(ssrc);
        PlayCache.MEDIA_SERVER_PLAYING.remove(ssrc);
        return ResponseEntity.ok(streamChanged);
    }
}
